package com.shjy.signdemo;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Main2Activity extends AppCompatActivity {

    private static final String TAG = "Main2Activity";

    private GestureOverlayView gesture;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        mContext = this;

        //获取手势编辑组件后，设置相关参数
        gesture = (GestureOverlayView) findViewById(R.id.gesture);
        gesture.setGestureColor(Color.GREEN);
        gesture.setGestureStrokeWidth(5);
        gesture.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {

            @Override
            public void onGesturePerformed(GestureOverlayView gestureOverlayView, final Gesture gesture) {

                View saveDialog = getLayoutInflater().inflate(R.layout.dialog_save, null, false);
                ImageView img_show = (ImageView) saveDialog.findViewById(R.id.img_show);
                final Bitmap bitmap = gesture.toBitmap(256, 256, 20, 0xffff0000);
                img_show.setImageBitmap(bitmap);

                new AlertDialog.Builder(mContext).setView(saveDialog)
                        .setPositiveButton("保存", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String lngID = new Date().getTime()+"";
                                // 获取文件对应的手势库
                                GestureLibrary mGestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/signDemo/gestures");
                                mGestureLibrary.load();
                                mGestureLibrary.addGesture(lngID, gesture);
                                mGestureLibrary.save();

                                saveBitmap(bitmap, lngID + ".jpg");

                                Intent mIntent = new Intent();
                                String str1="imgpath";
                                mIntent.putExtra(str1, lngID + ".jpg");
                                setResult(RESULT_OK, mIntent); //intent为A传来的带有Bundle的intent，当然也可以自己定义新的Bundle
                                finish();
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        finish();
                    }
                }).show();
            }
        });
    }

    /** 保存方法 */
    public void saveBitmap(Bitmap bitmap, String picName) {
        saveBitmapMore(bitmap,picName);
//        String stringUrl = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, picName, "签名文件");
//        if (!TextUtils.isEmpty(stringUrl)) {
//            SharedPreferences sharedPreferences = getSharedPreferences("sign", Context.MODE_PRIVATE);
//            SharedPreferences.Editor editor = sharedPreferences.edit();
//            editor.putString("imageUrl", stringUrl);
//            editor.commit();
//        }
    }



    public void saveBitmapMore(Bitmap bm, String picName) {
        Log.e(TAG, "保存图片");
        File f = new File("/mnt/sdcard/", picName);
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            Log.i(TAG, "已经保存");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
